<?php

class CommonApp extends Controller{

    public function _initialize() {
    	C('SHOW_PAGE_TRACE',(My::arr2var($_GET,'debug')=='true' OR C('SHOW_PAGE_TRACE')==true)?true:false);//debug调试
		//上一页面url
		define('URL_', (My::user('URL_'))?My::user('URL_'):empty($_SERVER['HTTP_REFERER'])?"":$_SERVER['HTTP_REFERER']);
		self::car();
    	self::setcookie();//设置相关cookie变量供前端用
	}
	
	/**
	 * 设置相关cookie变量供前端用
	 */
	private function setcookie(){
		setcookie("__ROOT__", __ROOT__,null,__ROOT__);
		setcookie("__PROJECT__", __PROJECT__,null,'/');
	}

	/**
	 * 跳转函数(兼容函数)
	 * by huanran.zhao
	 */
	function ajaxReturn($statusCode, $message, $callbackType = '', $forwardUrl = '', $data='') {
		//404页面,使用方法$this->ajaxReturn(404,'找不到页面');
		if($statusCode==404){
			$this->assign('title',$message);
			$this->display('404');
			exit();
		}
		//ajaxsubmit方式提交的表单(表单验证方式)
		if(My::is_ajax()){
			$jsonData = array(
								'statusCode' => $statusCode,
								'message' => $message,
								'forwardUrl' => $forwardUrl,
								'data' => $data
							 );
			exit(json_encode($jsonData));
		}
		//正常方式页面跳转
		else{
			if(isset($_GET['URL'])) $URL=$_GET['URL'];//将get赋值给post
			if(isset($_POST['URL_'])) $URL_=$_POST['URL_'];//将get赋值给post
			//有传参时,强行按这个跳转url=$url
			if(!empty($url)) $URL=$URL_=$url;
			if(My::user('URL_')) $URL_=My::user('URL_');
			//默认
			if(empty($URL)) $URL = URL;
			if(empty($URL_)) $URL_ = URL_;
			
			$this->assign("text",$message);
			$this->assign("time",2);
			if($statusCode==200){
				unset($_SESSION['URL_']);
				$this->assign("url",$URL_);//成功:返回上一页面URL_
			}else{
				$_SESSION['URL_'] = $URL_;
				$this->assign("url",$URL);//失败:返回上一页面
			}
			//debug调试控件
			MagicFactory::getInstance('PageTrace')->run();
			//输入json内容
			exit(json_encode(array('statusCode'=>$statusCode,'message'=>$message)));
		}
	}

	//购物车
	function car(){
		if(isset($_SESSION['member'])){
			$count = DB('car')->where("member_id = '{$_SESSION['member']['id']}'")->clear()->count();
			$this->assign('count',$count);
		}
	}

	//登陆验证
	function checklogin(){
		$member = My::arr2var($_SESSION,'member');
		if(!My::is_id(My::arr2var($member,'id'))){
			if(My::is_ajax())
                $this->ajaxReturn(201,'未登录!');
            else{
                setCookie("history_url", $_SERVER["REQUEST_URI"], time()+31536000, "/");
                redirect(Router::encodeUrl('index.php?app=Login'));
            }
		}
	}

    
    /**
     * 生成15位的订单号
     * @return string 订单号
     */
    function createOrderNm()

    {
        $year_code = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z');

        $date_code = array('0',

            '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A',

            'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M',

            'N', 'O', 'P', 'Q', 'R', 'T', 'U', 'V', 'W', 'X', 'Y');

        //一共15位订单号,同一秒内重复概率1/10000000,26年一次的循环\

        $order_sn = $year_code[(intval(date('Y')) - 2010) % 26] . //年 1位

            strtoupper(dechex(date('m'))) . //月(16进制) 1位

            $date_code[intval(date('d'))] . //日 1位

            substr(time(), -5) . substr(microtime(), 2, 5) . //秒 5位 // 微秒 5位

            sprintf('%02d', rand(0, 99)); //  随机数 2位
        return $order_sn;

    }
}